package jcataloger.gui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.List;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import jcataloger.beans.DiskImageInfo;
import jcataloger.beans.SearchResultBean;
import jcataloger.models.JCatalogerSearchTableModel;
import jcataloger.renderers.JCatalogerTableCellRenderer;
import jcataloger.resources.Messages;
import jcataloger.util.Indexer;
import jcataloger.util.Utils;

/**
 * This class opens the search screen in the project. It does all of the seach operations.
 * @author sekobey
 *
 */
public class SearchFrame extends JFrame {
	private JTextField searchField;
	private JCheckBox matchBox;
	private JButton okButton;
	private JTable resultTable;
	private JLabel statusBar;
	private MainFrame mainGui;
		
	/**
	 * default constructer
	 * @param owner
	 */
	public SearchFrame(MainFrame owner) {
		super(Messages.getString("SearchFrame.title"));
		mainGui = owner;
		init();
	}
	
	private void init() {
		Container c = getContentPane();
		c.add( getNorthPanel(), BorderLayout.NORTH );
		c.add( getCenterPanel(), BorderLayout.CENTER );
		c.add( getSouthPanel(), BorderLayout.SOUTH );
		
		pack();
		setSize(getWidth(), 300 );
		setLocationRelativeTo(mainGui);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	}
	
	private JPanel getNorthPanel() {
		ActionHanler handler = new ActionHanler(this);
		
		searchField = new JTextField(Messages.getString("SearchFrame.1"), 50);
		searchField.setLocale(new Locale("ISO-8859-9"));
		searchField.addFocusListener(
				new FocusAdapter() {
					public void focusGained(FocusEvent e) {
						searchField.selectAll();
					}
					public void focusLost(FocusEvent e) {
						searchField.selectAll();
					}
				}
		);
		searchField.addActionListener( handler );
		matchBox = new JCheckBox(Messages.getString("SearchFrame.2"));
		okButton = new JButton(Messages.getString("SearchFrame.3"));
		okButton.addActionListener( handler );
		
		JPanel panel = new JPanel();
		FlowLayout f = new FlowLayout(FlowLayout.LEFT);
		panel.setLayout(f);
		panel.setBorder( BorderFactory.createLineBorder(Color.GRAY));
		panel.add(searchField);
		panel.add(matchBox);
		panel.add(okButton);
		
		return panel;
	}
	
	private JPanel getCenterPanel() {
		JCatalogerSearchTableModel model = new JCatalogerSearchTableModel();
		resultTable = new JTable( model );
		resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		resultTable.setShowGrid(false);
		resultTable.setDefaultRenderer( SearchResultBean.class, new JCatalogerTableCellRenderer() );
		resultTable.getColumnModel().getColumn(0)
						.setCellRenderer(new JCatalogerTableCellRenderer());
		JTableHeader header = resultTable.getTableHeader();
		header.setVisible( false );
		JScrollPane scroller = new JScrollPane(resultTable);
		
		JPanel panel = new JPanel();
		BorderLayout b = new BorderLayout();
		panel.setLayout( b );
		panel.add( header, BorderLayout.NORTH );
		panel.add( scroller, BorderLayout.CENTER );
		
		return panel;
	}
	
	private JPanel getSouthPanel() {
		statusBar = new JLabel(" ");
		statusBar.setBorder(BorderFactory.createLoweredBevelBorder());
		
		JPanel panel = new JPanel();
		BorderLayout b = new BorderLayout();
		panel.setLayout( b );
		panel.add( statusBar );
		
		return panel;
	}
	
	/**
	 * Clears the form fields when the screen is set visible true again.
	 *
	 */
	public void clearForm() {
		setTitle(Messages.getString("SearchFrame.title"));
		searchField.setText(Messages.getString("SearchFrame.1"));
		statusBar.setText(" ");
		removeTableRows();
	}
	
	/**
	 * Search results are shown in a JTable. This method removes all the rows in the table.
	 *
	 */
	private void removeTableRows() {
		DefaultTableModel model = (DefaultTableModel)resultTable.getModel();
		while ( model.getRowCount() > 0 ) {
			model.removeRow(0);
		}
		
	}

	/**
	 * This method controls if the search field is empty
	 * @return
	 */
	private boolean isSearchFieldEmpty() {
		boolean result = Utils.isTextEmpty( searchField.getText() );
		if ( result )
			JOptionPane.showMessageDialog( this, Messages.getString("SearchFrame.13"),Messages.getString("title.warning"), JOptionPane.WARNING_MESSAGE);
		
		return result;
	}
	
	/**
	 * This class handles all actions
	 * @author sekobey
	 *
	 */
	class ActionHanler implements ActionListener {
		private SearchFrame searchGui;
		public ActionHanler(SearchFrame gui ) {
			searchGui = gui;
		}
		
		public void actionPerformed( ActionEvent e ) {
			if ( !isSearchFieldEmpty() ) {
				if ( e.getSource() == okButton ) {
					search(searchField.getText(), matchBox.isSelected());
				}
				else if ( e.getSource() == searchField ) {
					search(e.getActionCommand(), matchBox.isSelected());
				}
			}
			searchField.selectAll();
		}
		
		private void search(String text, boolean matchCase) {
			searchGui.setTitle(Messages.getString("SearchFrame.8", text));
			removeTableRows();
			List<DiskImageInfo> diskImageList = mainGui.getDiskImageList();
			if ( diskImageList.size() > 0 ) {
				Indexer indexer = Indexer.openIndex();
				List<SearchResultBean> result = indexer.searchText(text, matchCase);
				if ( result.size() > 0 ) {
					statusBar.setText(Messages.getString("SearchFrame.9", ""+result.size()));
					DefaultTableModel model = (DefaultTableModel)resultTable.getModel();
					resultTable.getTableHeader().setVisible(true);
					for ( SearchResultBean item : result ) {
						model.addRow( new Object[]{ item, item.getType(), item.getPath(), item.getDiskName(), item.getLength(), item.getExtension() } );
					}
				}
				else {
					statusBar.setText(Messages.getString("SearchFrame.15"));
					JOptionPane.showMessageDialog( searchGui, Messages.getString("SearchFrame.15"),Messages.getString("title.information"), JOptionPane.INFORMATION_MESSAGE);
				}
			}
			else
				JOptionPane.showMessageDialog( searchGui, Messages.getString("SearchFrame.12"),Messages.getString("title.warning"), JOptionPane.WARNING_MESSAGE);
			searchGui.setTitle(Messages.getString("SearchFrame.14", text));
		}

		

	}
}
